package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.CategoryMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    //查询所有
    @Override
    public Result findAll() {
        List<Category> list = categoryMapper.findAll();
        Result result = new Result<>();
        if (list.isEmpty()) {
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
        return new Result(false, StatusCode.OK, "查询成功", list);
    }

    //添加类别
    @Override
    public Result add(Category category) {
        int i = categoryMapper.insert(category);
        if (i > 0) {
            return new Result(true, StatusCode.OK, "success", null);
        }
        return new Result(false, StatusCode.ERROR, "插入失败", null);
    }

    //分页查询
    @Override
    public Result<Category> findPage(Integer page, Integer size) {
        IPage<Category> topage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<Category>(page, size);
        categoryMapper.selectPage(topage, null);
        //获取查询的记录
        List<Category> list = topage.getRecords();
        //获取总条数
        long total = topage.getTotal();
        //放到pageresult中
        PageResult<Category> tPageResult = new PageResult<Category>(total, list);
        if (list.isEmpty()) {
            return new Result(false, StatusCode.ERROR, "你查询到的数据为空", null);
        }
        return new Result(true, StatusCode.OK, "success", tPageResult);
    }

    //分页条件查询
    @Override
    public Result TfindPage(Integer page, Integer size, Category category) {
        IPage<Category> toPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, size);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //定义模糊查询条件
        if (category.getName() != null) {
            wrapper.like(Category::getName, category.getName());
        } else if (category.getIsMenu() != null) {
            wrapper.like(Category::getIsMenu, category.getIsMenu());
        } else if (category.getIsShow() != null) {
            wrapper.like(Category::getIsShow, category.getIsShow());
        } else if (category.getSeq() != null) {
            wrapper.like(Category::getParentId, category.getParentId());
        } else if (category.getSeq() != null) {
            wrapper.like(Category::getParentId, category.getParentId());
        }

        categoryMapper.selectPage(toPage, wrapper);
        //获取查询的记录
        List<Category> list = toPage.getRecords();
        //判断list集合中查没查到数据
        if (list.isEmpty()) {
            return new Result(false, null, "你查询到的数据为空", null);
        }
        //获取分页查询总条数
        long total = toPage.getTotal();
        //把数据放到pageresult中
        PageResult<Category> brandPageResult = new PageResult<Category>(total, list);
        return new Result(true, StatusCode.OK, "success", brandPageResult);
    }

    //根据id查找
    @Override
    public Result findById(Integer id) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId, id);
        Category category = categoryMapper.selectOne(wrapper);
        if (category == null) {
            return new Result(false, StatusCode.ERROR, "error", null);
        }
        return new Result(true, StatusCode.OK, "success", category);
    }

    //根据id修改
    @Override
    public Result updateById1(Category category, Integer id) {
        category.setId(id);
        int i = categoryMapper.updateById(category);
        //返回结果
        if (i > 0) {
            return new Result(true, StatusCode.OK, "success", null);
        }
        return new Result(false, StatusCode.ERROR, "error", null);
    }

    //根据id删除
    @Override
    public Result deleteById(Integer id) {
        int i = categoryMapper.deleteById(id);
        if (i > 0) {
            return new Result(true, StatusCode.OK, "success", null);
        }
        return new Result(false, StatusCode.ERROR, "error", null);
    }


}
